﻿using System;

namespace Tables
{
    class List : IDisposable
    {
        public List()
        {
            _size = 0;
            _pHead = null;
        }
        /// <summary>
        /// Adds to the front of the list
        /// </summary>
        /// <param name="id"></param>
        public void Add(int id)
        {
            Link pLink = new Link(_pHead, id);
            _pHead = pLink;
            ++_size;
        }
        public int Count { get { return _size; } }
        public Link GetHead()
        {
            return _pHead;
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    //disposed managed resource
                    while (_size != 0)
                    {
                        Link pLink = _pHead;
                        _pHead = _pHead.Next();
                        pLink.Dispose();
                        --_size;
                    }
                }
                // release unmanaged resource
            }
            _disposed = true;

            // if available, call to base class
            // base.Dispose(disposing);
        }

        private Link _pHead;
        private int _size;
        private bool _disposed = false;
    }
}
